package com.ruoyi.project.product.images.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.images.domain.Images;
import com.ruoyi.project.product.images.service.IImagesService;
import com.ruoyi.project.product.url.domain.Url;
import com.ruoyi.project.product.url.service.IUrlService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品图片操作处理
 * @author Mai
 */
@Controller
@RequestMapping("product/images")
public class ImagesController extends BaseController {

    private String prefix="product/images";

    @Autowired
    private IImagesService imagesService;

    @Autowired
    private IUrlService urlService;

    /**
     * 跳转到查询页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("product:images:view")
    public String images(){
        return prefix + "/images";
    }

    /**
     * 产品图片查询
     * @param images
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("product:images:list")
    public TableDataInfo list(Images images){
        startPage();
        List<Images> list = imagesService.selectImagesList(images);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap){
        mmap.put("url",urlService.selectUrlList(new Url()));
        return prefix + "/add";
    }

    /**
     * 产品图片新增
     * @param images
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("product:images:add")
    public AjaxResult addSave(Images images){
        return toAjax(imagesService.insertImages(images));
    }

    /**
     * 图片修改
     * @param imagesId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{imagesId}")
    public String edit(@PathVariable("imagesId") Long imagesId, ModelMap mmap){
        mmap.put("images",imagesService.selectImagesById(imagesId));
        mmap.put("url",urlService.selectUrlList(new Url()));
        return prefix + "/edit";
    }

    /**
     * 图片修改保存
     * @param images
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions("product:images:edit")
    public AjaxResult editSave(Images images){
        return toAjax(imagesService.updateImages(images));
    }



    /**
     * 图片批量删除操作
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("product:images:remove")
    public AjaxResult remove(String ids){
        try {
            return toAjax(imagesService.deleteImagesByIds(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * dom操作更改图片
     * @param urlId
     * @return
     */
    @ResponseBody
    @PostMapping("/changeImagesUrl")
    public AjaxResult changeImagesUrl(Integer urlId){
        return AjaxResult.success(urlService.selectUrlById(urlId));
    }



}
